package com.sssd.ui;

import cn.hutool.core.util.StrUtil;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;

/**
 * @author sssd
 * @careate 2023-02-20-10:12
 */
public class WindowUi {

    private Integer numKey;

    private Integer delayTime;

    private static Map<Integer, Integer> defaultNumKeys;

    static {
        HashMap<Integer, Integer> keys = new HashMap<Integer, Integer>(10);
        int j = 1;
        for (int i = NativeKeyEvent.VC_1; i <= NativeKeyEvent.VC_9; i++) {
            keys.put(j, i);
            j++;
        }
        defaultNumKeys = keys;
    }

    public Integer getNumKey() {
        return numKey;
    }

    public void setNumKey(Integer numKey) {
        this.numKey = numKey;
    }

    public Integer getDelayTime() {
        return delayTime;
    }

    public void setDelayTime(Integer delayTime) {
        this.delayTime = delayTime;
    }

    public void init() {
        Frame frame = new Frame("NBA2K-Online");
        //可见窗口
        frame.setVisible(true);
        //设置窗口宽高
        frame.setSize(200, 300);
        //设置背景色
//        frame.setBackground(new Color(50, 50, 50));
        //不能随意变动大小
        frame.setResizable(false);

        Dialog tipDialog = new Dialog(frame, "Tips", true);
        tipDialog.setSize(100, 150);
        tipDialog.setLocation((int) (frame.getWidth() - tipDialog.getWidth()) / 2, (int) (frame.getHeight() - tipDialog.getHeight()) / 2);
        tipDialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                tipDialog.setVisible(false);
            }
        });
        //窗口初始化时的位置
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) (screenSize.getWidth() - frame.getWidth()) / 2;
        int y = (int) (screenSize.getHeight() - frame.getHeight()) / 2;
        frame.setLocation(x, y);
        //布局
        GridLayout gridLayout = new GridLayout();
        gridLayout.setRows(3);
        frame.setLayout(gridLayout);
        //构建表单
        Panel numKeyPanel = new Panel();
        numKeyPanel.add(new Label("投篮绑定键:"));
        TextField numKeyInput = new TextField();
        numKeyPanel.add(numKeyInput);
        frame.add(numKeyPanel);

        Panel delayTimePanel = new Panel();
        delayTimePanel.add(new Label("投篮延迟(ms):"));
        TextField delayTimeInput = new TextField();
        delayTimePanel.add(delayTimeInput);
        frame.add(delayTimePanel);

        Panel submitPanel = new Panel();
        Button submitButton = new Button("确定");
        submitPanel.add(submitButton);
        frame.add(submitPanel);


        submitButton.addActionListener(submit -> {
            String numKey = numKeyInput.getText();
            if (StrUtil.isBlank(numKey)) {
                tipDialog.add(new Label("输入的绑定键不能为空"));
                tipDialog.setVisible(true);
                return;
            }
            int inputNumKey = 0;
            try {
                inputNumKey = Integer.parseInt(numKey);
            } catch (NumberFormatException e) {
                // FIXME: 2023/2/20
                tipDialog.add(new Label("输入的绑定键必须是1-9之间的数字键"));
                tipDialog.setVisible(true);
                return;
            }
            if (!defaultNumKeys.containsKey(inputNumKey)) {
                // FIXME: 2023/2/20
                tipDialog.add(new Label("输入的绑定键必须是1-9之间的数字键"));
                tipDialog.setVisible(true);
                return;
            }
            setNumKey(defaultNumKeys.get(inputNumKey));
            System.out.println("绑定的key是: " + defaultNumKeys.get(inputNumKey));

            String delayTime = delayTimeInput.getText();
            if (StrUtil.isBlank(numKey)) {
                tipDialog.add(new Label("输入的延迟时间不能为空"));
                tipDialog.setVisible(true);
                return;
            }
            int inputDelayTimeKey = 0;
            try {
                inputDelayTimeKey = Integer.parseInt(delayTime);
            } catch (NumberFormatException e) {
                // FIXME: 2023/2/20
                tipDialog.add(new Label("输入的延迟时间必须是数字"));
                tipDialog.setVisible(true);
                return;
            }
            setDelayTime(inputDelayTimeKey);
        });


        //窗口关闭
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
